package net.ib.mtalk.account;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.RawContacts;

/**
 * Contact 을 관리하는 class
 * @author namkhoh
 *
 */
public class ContactManager {
	
	/*public static synchronized void syncContacts(Context context, String account, ArrayList<Buddy> buddies) {
		
		final ContentResolver mResolver = context.getContentResolver();
		final BatchOperation batchOperation = new BatchOperation(context, mResolver);
		
		long rawContactId = 0;
		String number;
		for (Buddy buddy : buddies) {
			number = buddy.value;
			rawContactId = lookupRawContact(mResolver, number);
			
			if (rawContactId != 0) {
				// UPDATE or DELETE
				if (buddy.deleted) {
					// DELETE Buddy
				} else {
					// UPDATE Buddy
					updateContact(context, buddy, rawContactId, batchOperation);
				}
			} else {
				// ADD NEW BUDDY
				if (!buddy.deleted) {
					long contactId = lookupContact(mResolver, buddy.orgnum);
					if (contactId != 0) {
						addContact(context, buddy, batchOperation);
					}
				} 
			}
			
			if (batchOperation.size() > 50) {
				batchOperation.execute();
			}
			
		}
		batchOperation.execute();
	}
	
	private static void addContact(Context context, Buddy buddy, BatchOperation batchOperation) {
		final ContactOperations contactOp = ContactOperations.createNewContact(context, buddy.value, batchOperation);
		contactOp.addName(lookupName(context.getContentResolver(), buddy.orgnum)).addProfileAction(buddy);
	}
	
	private static void updateContact(Context context, Buddy buddy, long rawContactId, BatchOperation batchOperation) {
		final ContactOperations contactOp = ContactOperations.updateExistingContact(context, rawContactId, batchOperation);
		contactOp.addName(lookupName(context.getContentResolver(), buddy.orgnum));
	 }
	
	*//**
	 *  Buddy 번호를 가지고 Contact 에 추가된 사용자 인지 조회한다. 
	 * @param resolver
	 * @param number
	 * @return
	 *//*
	private static long lookupRawContact(ContentResolver resolver, String number) {
		long authorId = 0;
		final Cursor c = resolver.query(RawContacts.CONTENT_URI, UserIdQuery.PROJECTION, 
										UserIdQuery.SELECTION, new String[]{number}, null);
		
		try {
			if (c.moveToFirst()){
				authorId = c.getLong(UserIdQuery.COLUMN_ID);
			}
		} finally {
			if (c != null) {
				c.close();
			}
		}
		
		return authorId;
	}
	
	private static String lookupName(ContentResolver resolver, String number) {
		String name = number;
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
		final Cursor c = resolver.query(uri, null, null, null, null);
		try {
			if (c.moveToFirst()) {
				name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
			}
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return name;
	}
	
	*//**
	 * Number 를 가지고 Contact 을 조회한다. 
	 * @param resolver
	 * @param number
	 * @return
	 *//*
	private static long lookupContact(ContentResolver resolver, String number) {
		
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
		
		long contactId = 0;
		final Cursor c = resolver.query(uri, ContactIdQuery.PROJECTION, null, null, null);
		try {
			if (c.moveToFirst()) {
				contactId = c.getLong(ContactIdQuery.COLUMN_ID);
			}
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return contactId;
	}
	
	private interface ContactIdQuery {
		public final static String[] PROJECTION = new String[] {ContactsContract.Contacts._ID};
		
		public final int COLUMN_ID = 0;				
	}
	
	private interface UserIdQuery {
		public final static String[] PROJECTION = new String[] {RawContacts._ID, RawContacts.ACCOUNT_NAME};
		
		public final int COLUMN_ID = 0;
		
		public static final String SELECTION = 
				RawContacts.ACCOUNT_TYPE + "='" + ChatONConst.ACCOUNT_TYPE + "' AND "
					+ RawContacts.ACCOUNT_NAME + "=? AND " + RawContacts.DELETED +"=0";
	}*/
}
